﻿using log4net.Repository.Hierarchy;
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Runtime.InteropServices;

namespace IntoTheVoid.Units
{
	public static class LogHelper
	{
		private static readonly ILog _log = LogManager.GetLogger(typeof(Logger));
		public static void Error(string msg) => _log.Error(AppendClassLine(msg));
		public static void Error(Exception ex, string msg = null) {
			Common.ReportException("Error", msg, ex);
			_log.Error(msg, ex);
		} 

		public static void Info(string msg) => _log.Info(AppendClassLine(msg));
		public static void Info(Exception ex, string msg = null) => _log.Info(msg, ex);

		public static void Debug(string msg) => _log.Debug(AppendClassLine(msg));
		public static void Debug(Exception ex, string msg = null) => _log.Debug(msg, ex);

		public static void Warn(string msg) => _log.Warn(AppendClassLine(msg));
		public static void Warn(Exception ex, string msg = null) {
			Common.ReportException("Warn", msg, ex);
			_log.Warn(msg, ex);
		}

		public static void Fatal(string msg) => _log.Warn(AppendClassLine(msg));
		public static void Fatal(Exception ex, string msg = null) => _log.Fatal(msg, ex);

		static string AppendClassLine(string msg)
		{
			string logStr = msg;
			try
			{
				StackTrace st = new StackTrace(true);
				StackFrame sf = st.GetFrame(2);
				logStr = $"{msg} [{Path.GetFileName(sf.GetFileName())}:{sf.GetFileLineNumber().ToString()}]";
			}
			catch { }
			return logStr;
		}
/*		[Conditional("DEBUG")]
		public static void Log(string s)
		{
			Trace.WriteLine(s);
		}

		[Conditional("DEBUG")]
		public static void LogError(string s)
		{
			Console.ForegroundColor = ConsoleColor.Red;
			Trace.WriteLine(s);
			Console.ResetColor();
		}

		[Conditional("DEBUG")]
		public static void LogWarn(string s)
		{
			Console.ForegroundColor = ConsoleColor.Yellow;
			Trace.WriteLine(s);
			Console.ResetColor();
		}*/
		public static int GetLineNum()
		{
			System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
			return st.GetFrame(0).GetFileLineNumber();
		}

		public static string GetCurSourceFileName()
		{
			System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
			return st.GetFrame(0).GetFileName();
		}
	}


}
